package com.lancoo.klgcourseware.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.Html;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ReplacementSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

import com.lancoo.klgcourseware.utils.KlgToolUtils;



/**
 * 自定义的Span,用来绘制重要知识点标记
 */
public class UsageReplaceSpan extends ReplacementSpan {

    private Paint mPaint;//绘制重要知识点文本
    private Context context;
    private int tag=0;
    private Path path;
    private Paint paint_underline;//绘制重要知识点文本的下划线
    private int lineColor = 0xff282828;
    private String wordId="";
    private String wordName="";
    private int drawableLineType;//0-默认画线，1-画红线，2-不换线

    public void setTag(int tag){
        this.tag=tag;
    }


    public UsageReplaceSpan(Context context, Paint paint) {
        this.context=context;
        this.mPaint=paint;
        path = new Path();
        paint_underline=new Paint();
        paint_underline.setAntiAlias(true);
        paint_underline.setStrokeWidth(2.0f);
        paint_underline.setStyle(Paint.Style.STROKE);
        paint_underline.setColor(lineColor);
//        DashPathEffect effects =
//                new DashPathEffect(new float[] {ToolsUtils.dip2px(context, 6) * 0.6f,
//                        ToolsUtils.dip2px(context, 6) * 0.4f,
//                        ToolsUtils.dip2px(context, 6) * 0.6f,
//                        ToolsUtils.dip2px(context, 6) * 0.4f}, 1);
//        paint_underline.setPathEffect(effects);

    }


    public int padding=0;
    public int id = 0;//回调中的对应Span的ID
    private int mWidth = 0;//最长单词的宽度
    public String mText;//保存的String


    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        //将返回相对于Paint画笔的文本 50== 左右两边增加的空余长度
        if (mWidth == 0)mWidth = (int) paint.measureText(mText, 0, mText.length());
        return mWidth;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        //填入对应单词
//        Log.e("draw","start:"+start+",end:"+end+",x:"+x+",top:"+top+",y:"+y+"bottom:"+bottom);
        if (!TextUtils.isEmpty(mText)){
            int width = (int)mPaint.measureText(mText,0,mText.length());
            width = (mWidth-width)/2;
//            Log.e("draw","draw text x:"+(x+width)+".....y:"+((float)(y)- padding));

            if(drawableLineType==2){
                canvas.drawText(mText,0,mText.length(),x+width,(float)(y),mPaint);
            }else{
                canvas.drawText(mText,0,mText.length(),x+width,(float)(y)- padding,mPaint);
            }
        }
        if(drawableLineType==2){
            paint_underline.setColor(0x00000000);
        }
        if(drawableLineType==1){
            paint_underline.setColor(0xff333333);
        }
        int dp2=KlgToolUtils.dip2px(context,0);
        path.moveTo(x+dp2,y+ KlgToolUtils.dip2px(context,4));
        path.lineTo( x + mWidth-dp2,y+ KlgToolUtils.dip2px(context,4));
//        Log.e("draw","draw line x:"+x+".....y:"+y+"....mWidth:"+mWidth);
//        canvas.drawLine(x, y+ ToolsUtils.dip2px(context,4), x + mWidth, y+ ToolsUtils.dip2px(context,4), mPaint);
        if(drawableLineType!=2){
            canvas.drawPath(path,paint_underline);
        }

    }

    //TextView触摸事件-->Span点击事件
    public static LinkMovementMethod Method = new LinkMovementMethod() {

        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {

            int action = event.getAction();
            if (action == MotionEvent.ACTION_UP ||
                    action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();
                x += widget.getScrollX();
                y += widget.getScrollY();
                Layout layout = widget.getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

            }
            return false;
        }


        @Override
        public void initialize(TextView widget, Spannable text) {
            Selection.removeSelection(text);
        }
    };

    public void setDrawTextColor(int res){
        mPaint.setColor(res);
        paint_underline.setColor(res);
    }
    public void setDrawTextColor(String res){
        mPaint.setColor(Color.parseColor(res));
        paint_underline.setColor(Color.parseColor(res));
    }

    public String getWordId() {
        return wordId;
    }

    public void setWordId(String wordId) {
        this.wordId = wordId;
    }

    public String getWordName() {
        return wordName;
    }

    public void setWordName(String wordName) {
        this.wordName ="   "+ wordName+"   ";
    }

    public void setmText(String mText) {
        this.mText ="   "+ Html.fromHtml(mText)+"   ";
    }

    public void setmTextNoFill(String mText) {
        this.mText =""+ Html.fromHtml(mText)+" ";
    }

    public void setSubText(String mText) {
        this.mText =" " +Html.fromHtml(mText);
    }

    public void setDrawableLineType(int drawableLineType){
        this.drawableLineType=drawableLineType;
    }

}
